package com.jkoss.wine.warehouse.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.merchant.entity.MerchantMerchantProductNorm;
import com.jkoss.wine.merchant.service.IMerchantProductNormService;
import com.jkoss.wine.system.entity.Role;
import com.jkoss.wine.warehouse.entity.WareInventory;
import com.jkoss.wine.warehouse.service.IWareInventoryService;
import com.jkoss.wine.warehouse.service.IWarehouseService;
import com.jkoss.wine.warehouse.vo.WareInventoryVo;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

/**
 * 仓库库存 前端控制器
 * 
 * @Author Ning
 * @Version 1.0, 2018-10-19
 * @See
 * @Since com.jkoss.wine.warehouse.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/warehouse/wareInventory")
public class WareInventoryController extends BaseController {

	@Autowired
	private IWareInventoryService iWareInventoryService;
	@Autowired
	private IWarehouseService iWarehouseService;
	@Autowired
	private IMerchantProductNormService iMerchantProductNormService;

	/**
	 * 列表
	 * 
	 * @param dwzPageBean
	 * @param wname
	 * @param normName
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/warehouse/wareInventory/list")
	public String list(DwzPageBean dwzPageBean, String wname, String normName, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		String normname = request.getParameter("org2.normName");
		Wrapper wrapper2 = Condition.create().ne("del", 1);
		map.put("warehouses", iWarehouseService.selectList(wrapper2));
		Wrapper wrapper = Condition.create().ne("wi.del", 1).like("whid", wname).like("mpn.norm_name", normname);
		dwzPageBean.getCountResultMap().put("wname", wname);
		dwzPageBean.getCountResultMap().put("normName", normname);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("amut", false);
		}
		Page resultPage = iWareInventoryService.selectVoList(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "warehouse/wareInventory/list";
	}

	/**
	 * 去新增
	 * 
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/warehouse/wareInventory/toInsert")
	public String toInsert(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		return "warehouse/wareInventory/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/warehouse/wareInventory/toInsert")
	@ResponseBody
	public Object insert(@Valid WareInventory ware_inventory, HttpServletRequest request, HttpServletResponse response) {
		if (iWareInventoryService.insert(ware_inventory)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/warehouse/wareInventory/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iWareInventoryService.selectById(id));
		return "warehouse/wareInventory/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/warehouse/wareInventory/toUpdate")
	@ResponseBody
	public Object update(@Valid WareInventory ware_inventory, HttpServletRequest request, HttpServletResponse response) {
		if (iWareInventoryService.updateById(ware_inventory)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/warehouse/wareInventory/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		if (iWareInventoryService.deleteBatchIds(Arrays.asList(id))) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 查询产品规格名称
	 * 
	 * @param normName
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getNormName")
	@ResponseBody
	public Object getNormName(String normName, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("norm_name", normName).ne("del", 1);
		List<MerchantMerchantProductNorm> list = iMerchantProductNormService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 导出EXCEL
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/export")
	@RequiresPermissions("/warehouse/wareInventory/export")
	public String export(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create().ne("wi.del", 1);
		// 根据用户id获取角色
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if ("运营主管".equals(role.getName())) {
				isDirector = false;
			}
		}
		if (isDirector) {
			wrapper.eq("maid", getUserId());
		}
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iWareInventoryService.selectVoList(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "仓库库存", ExcelType.XSSF);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, WareInventoryVo.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "仓库库存");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

}
